package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Role;
import com.mall.db.service.RoleService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/system/roc")
public class RoleController extends BaseController<Role> {

    @Resource(name = "roleService")
    private RoleService ros;

    @Override
    public Map<String, Object> find(Role role) {
        PageHelper.startPage(role.getPage(), role.getLimit());
        QueryWrapper<Role> qw = new QueryWrapper<>();
        List<Role> list = ros.list(qw);
        PageInfo<Role> page = new PageInfo<>(list, role.getLimit());
        return this.findMap(page.getList(), page.getTotal());
    }


    @Override
    public Map<String, Object> add(Role role) {
        role.setCreateTime(new Date());
        boolean r = ros.save(role);
        return this.iud("新增", r, role);
    }

    @Override
    public Map<String, Object> update(Role role) {
        boolean r = ros.updateById(role);
        return this.iud("编辑", r, role);
    }


    @Override
    public Map<String, Object> delete(Role role) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(role.getId())) {
            ros.removeById(role.getId());
            map.put("is", true);
            map.put("msg", "删除成功!");
        } else {
            map.put("is", false);
            map.put("msg", "删除失败!");
        }
        return map;
    }

}
